﻿<%@ WebHandler Language="C#" Class="addType" %>

using System;
using System.Web;
using System.Data.OleDb;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web.Security;
using System.Xml.Linq;
using System.Web.Script.Serialization;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

public class addType : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        //准备回送的字符串
        string outstr = "";
        //接收数据
        string username = context.Request.Form["username"];
        string limit = context.Request.Form["auth"];

        //执行写入操作
        outstr = doInsert(username, limit);

        //回送结果
        context.Response.Write(outstr);
    }
    public string doInsert(string username, string limit)
    {
        int i = 0;
       
        string strPwChar = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        //截取长度为8.
        string password = MakePassword(strPwChar, 8);
        DataSet myds = DBHelper.sqlMethod("select * from admins where username = '" + username + "'");
        int users = myds.Tables[0].Rows.Count;
        if (users > 0)
        {
            return "{\"r\":\"0\", \"data\":\"用户已存在！\"}";
        }
        if (username != "")
        {
            //执行插入操作，并获得影响的行数
            i = DBHelper.sqlMethodNoDataset("insert into admins(username,password,limit) values('" + username + "','" + MD5.getMD5(password) + "','" + limit + "');");
        }
        return "{\"r\":\"" + i + "\", \"data\":\"" + password + "\"}";
    }
    /// <summary>
    /// 生成随机字符串
    /// </summary>
    /// <param name="strPwChar">传入生成的随机字符串可以使用哪些字符</param>
    /// <param name="intlen">传入生成的随机字符串的长度</param>
    public static string MakePassword(string strPwChar, int intlen)
    {
        string strRe = "";
        int iRandNum;
        Random rnd = new Random();
        for (int i = 0; i < intlen; i++)
        {
            iRandNum = rnd.Next(strPwChar.Length);
            strRe += strPwChar[iRandNum];
        }
        return strRe;
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}